Pay = require 'weixin-pay'
request = require 'request'
fs = require 'fs'
path = require 'path'
pay  =  Pay
	appid: process.env.FUWUHAOID
	mch_id: process.env.MCHID
	partner_key: process.env.MCHKEY
	pfx: if process.env.MCHPFX then  fs.readFileSync path.resolve __dirname, '../certs/' + process.env.MCHPFX else ''

console.log
	appid: process.env.FUWUHAOID
	mch_id: process.env.MCHID
	partner_key: process.env.MCHKEY
	pfx: if process.env.MCHPFX then  fs.readFileSync path.resolve __dirname, '../certs/' + process.env.MCHPFX else ''



pay.transfer = (body)->
	body.nonce_str = pay.Util.generateNonceString()
	body.mch_appid = pay.wxpayID.appid
	body.mchid = pay.wxpayID.mch_id
	body.sign = pay.sign body
	console.log body
	xml = pay.Util.buildXML xml: body

	return new Promise (resolve, reject)->
		request
			url: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'
			method: "POST"
			body: xml
			agentOptions:
				pfx: pay.options.pfx
				passphrase: pay.options.mch_id
		, (err, res, body)->
			return reject err if err
			resolve body

module.exports = pay